﻿using System.Linq;
using WJSHWebApi.DBModel;
using WJSHWebApi.Interface;

namespace WJSHWebApi.Server
{
    public class EmptyCacheSelectServer
    {
        IDBInterface iDBInterface;
        public EmptyCacheSelectServer(IDBInterface _iDBInterface)
        {
            iDBInterface = _iDBInterface;
        }
        public EmptyCache_Entity GetHave()
        {
            EmptyCache_Entity Result= null;
            var List = iDBInterface.Where<EmptyCache_Entity>(x=>x.IsCreat==false && x.IsEnable==true && x.IsHave==true);
            foreach (var item in List) 
            {
                var templist = iDBInterface.Where<EmptyCache_Entity>(x => x.IsCreat == false && x.IsEnable == true && x.IsHave == false && x.Row== item.Row && x.Col< item.Col);
                if (templist.Count()== item.Col-1)
                {
                    Result = item;
                    break;
                }
            }

            return Result;
        }

        public EmptyCache_Entity GetEmpty()
        {
            EmptyCache_Entity Result = null;
            var List = iDBInterface.Where<EmptyCache_Entity>(x => x.IsCreat == false && x.IsEnable == true && x.IsHave == false);
            foreach (var item in List)
            {
                var templist = iDBInterface.Where<EmptyCache_Entity>(x => x.IsCreat == false && x.IsEnable == true && x.IsHave == false && x.Row == item.Row && x.Col < item.Col);
                var templist2 = iDBInterface.Where<EmptyCache_Entity>(x => x.IsCreat == false && x.IsEnable == true && x.IsHave == true && x.Row == item.Row && x.Col > item.Col);
                if (templist.Count() == item.Col - 1 && (templist2.Count()+ item.Col)== item.RowCount)
                {
                    Result = item;
                    break;
                }
            }

            return Result;
        }


        public bool BoolGetEmpty(int Type) 
        {
            var List = iDBInterface.Where<EmptyCache_Entity>(x => x.IsEnable == true && x.IsHave == true);
            var TaskList = iDBInterface.Where<Task_Entity>(x => x.TaskType_TaskType==Type);
            if (List.Count()< TaskList.Count()+1)
            {
                return false;
            }
            return true;
        }
        public bool BoolPutEmpty(int Type)
        {
            var List = iDBInterface.Where<EmptyCache_Entity>(x => x.IsEnable == true && x.IsHave == false);
            var TaskList = iDBInterface.Where<Task_Entity>(x => x.TaskType_TaskType == Type);
            if (List.Count() < TaskList.Count() + 1)
            {
                return false;
            }
            return true;
        }
    }
}
